/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.mzk.kramerius.cleaner;

import cz.mzk.kramerius.fedoraaccess.FedoraConnector;
import cz.mzk.rehan.commons.Dom4jUtils;
import java.util.List;
import org.dom4j.XPath;

/**
 *
 * @author Martin Řehánek (rehan at mzk.cz)
 */
public class MonographRemover extends AbstractRemover {

    private final XPath monographUnitPidsXpath = Dom4jUtils.createXPath("/rdf:RDF/rdf:Description/kramerius:hasUnit/@rdf:resource");

    public MonographRemover(FedoraConnector con) {
        super(con);
    }

    public void removeMonograph(String monographPid) {
        System.out.println("monograph pid: '" + monographPid + "'");
        removeInternalParts(monographPid);
        removeMonographUnits(monographPid);
        removePages(monographPid);
        purgeDigitalObject(monographPid);
    }

    private void removeMonographUnits(String monographPid) {
        List<String> unitPidList = findPidsOfMonographUnits(monographPid);
        System.out.println("monograph units: " + unitPidList.size());
        for (String unitPid : unitPidList) {
            removeMonographUnit(unitPid);
        }
    }

    private List<String> findPidsOfMonographUnits(String monographPid) {
        return findPidsOfReferencedEntities(monographPid, monographUnitPidsXpath);
    }

    private void removeMonographUnit(String unitPid) {
        removeInternalParts(unitPid);
        removePages(unitPid);
        purgeDigitalObject(unitPid);
    }
}
